package com.dawn.shiro;

import cn.hutool.core.bean.BeanUtil;
import com.dawn.entity.User;
import com.dawn.service.UserService;
import com.dawn.utils.JwtUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AccountRealm extends AuthorizingRealm {
    @Autowired
    JwtUtils jwtUtils;

    @Autowired
    UserService userService;

    @Override//为了让realm支持jwt凭证校验
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }


    @Override//权限校验
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    @Override//登录认证校验
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        JwtToken jwtToken = (JwtToken) authenticationToken;
        String userId = jwtUtils.getClaimByToken((String) jwtToken.getPrincipal()).getSubject();
        User user = userService.getById(Long.valueOf(userId));
        if(user == null){
            throw  new UnknownAccountException("账户不存在");
        }
        if(user.getStatus() == -1){
            throw  new LockedAccountException("账户已被锁定");
        }
        //能公开的基本信息存到AccountProfile里面
        AccountProfile accountProfile = new AccountProfile();
        BeanUtil.copyProperties(user,accountProfile);
        return new SimpleAuthenticationInfo(accountProfile,jwtToken.getCredentials(),getName());
    }
}
